import {post, get} from '../utils/';
import * as types from '../constants/userTypes';

/**
 * 登录
 * */
export function login(data) {
    return (dispatch) => {
        return post('/api/login',data).then(
            response => dispatch({
                type: types.LOGIN,
                data: response
            }),
            error => dispatch({
                type: types.LOGIN_FAILED,
                error
            })
        )
    }
}

/**
 * 绑定手机
 * */
export function bindPhone(data) {
    return (dispatch) => {
        return post('/api/person/bindPhone',data).then(
            response => dispatch({
                type: types.BIND_PHONE,
                data: response
            }),
            error => dispatch({
                type: types.BIND_PHONE_FAILED,
                error
            })
        )
    }
}

/**
 * 获取用户信息
 * */
export function fetchUserInfo() {
    return (dispatch) => {
        return post('/api/person/info').then(
            response => dispatch({
                type: types.FETCH_USER_INFO,
                userInfo: response.data
            }),
            error => dispatch({
                type: types.FETCH_USER_INFO_FAILED,
                error
            })
        )
    }
}

/**
 * 修改用信息
 * */
export function changeUserInfo(data) {
    return (dispatch) => {
        return post('/api/person/editInfo',data).then(
            response => dispatch({
                type: types.CHANGE_USER_INFO,
                data: response
            }),
            error => dispatch({
                type: types.CHANGE_USER_INFO_FAILED,
                error
            })
        )
    }
}

/**
 * 更换手机
 * */
export function changePhoneNumber(data) {
    return (dispatch) => {
        return post('/api/person/changePhone',data).then(
            response => dispatch({
                type: types.CHANGE_PHONE_NUMBER,
                data: response
            }),
            error => dispatch({
                type: types.CHANGE_PHONE_NUMBER_FAILED,
                error
            })
        )
    }
}

/**
 * 发送验证码
 * */
export function sendSmsCode(data) {
    return (dispatch) => {
        return post('/api/captcha',data).then(
            response => dispatch({
                type: types.SEND_SMS_CODE,
                data: response
            }),
            error => dispatch({
                type: types.SEND_SMS_CODE_FAILED,
                error
            })
        )
    }
}


/**
 * 获取家庭成员列表
 * */
export function fetchFamList() {
    return (dispatch) => {
        return post('/api/family/list').then(
            response => dispatch({
                type: types.FETCH_FAM_LIST,
                data: response
            }),
            error => dispatch({
                type: types.FETCH_FAM_LIST_FAILED,
                error
            })
        )
    }
}

/**
 * 新增家庭成员
 * @param {string} name  姓名
 * @param {string} relation  关系
 * @param {int} sex  性别
 * @param {int} birthday  生日
 * @param {string} phone  手机号码
 * */
export function addFamMember(name,relation, sex, birthday, phone) {
    let data = {
        name: name,
        relation: relation,
        sex: sex,
        birthday: birthday,
        phone: phone
    };
    return dispatch => {
        return post('/api/family/add',data).then(
            response => dispatch({
                type: types.ADD_FAM_MEMBER,
                data:response,
                member: data
            }),
            error => dispatch({
                type: types.ADD_FAM_MEMBER_FAILED,
                error
            })
        )
    }
}

/**
 * 修改家庭成员
 * @param {int} id  成员id
 * @param {string} name  姓名
 * @param {string} relation  关系
 * @param {int} sex  性别
 * @param {int} birthday  生日
 * @param {string} phone  手机号码
 * */
export function editFamMember(id,name,relation, sex, birthday,phone) {
    let data = {
        familyId: id,
        name: name,
        relation: relation,
        sex: sex,
        birthday: birthday,
        phone: phone
    };
    return dispatch => {
        return post('/api/family/edit',data).then(
            response => dispatch({
                type: types.EDIT_FAM_MEMBER,
                data: response,
                member: data
            }),
            error => dispatch({
                type: types.EDIT_FAM_MEMBER_FAILED,
                error
            })
        )
    }
}

/**
 * 删除家庭成员
 * @param {int} id  成员id
 * */
export function delFamMember(id) {
    let data = {
        familyId: id
    };
    return dispatch => {
        return post('/api/family/delete',data).then(
            response => dispatch({
                type: types.DEL_FAM_MEMBER,
                data: response,
                memberId: id
            }),
            error => dispatch({
                type: types.DEL_FAM_MEMBER_FAILED,
                error
            })
        )
    }
}


/**
 * 添加活动订单
 * */
// TODO 删除
// export function addOrder(data) {
//     return dispatch => {
//         return post('/api/activity/event/order/add',data).then(
//             response => dispatch({
//                 type: types.ADD_ORDER,
//                 data: response
//             }),
//             error => dispatch({
//                 type: types.ADD_FAM_MEMBER_FAILED,
//                 error
//             })
//         )
//     }
// }

/**
 * 支付订单
 * */
// TODO 删除
// export function payOrder(data) {
//     return dispatch => {
//         return post('/api/activity/event/order/pay',data).then(
//             response => dispatch({
//                 type: types.PAY_ORDER,
//                 data: response
//             }),
//             error => dispatch({
//                 type: types.PAY_ORDER_FAILED,
//                 error
//             })
//         )
//     }
// }
export function payOrder(data) {
    return dispatch => {
        return post('/api/activity/event/order',data).then(
            response => dispatch({
                type: types.PAY_ORDER,
                data: response
            }),
            error => dispatch({
                type: types.PAY_ORDER_FAILED,
                error
            })
        )
    }
}




/**
 * 获取订单列表
 * */
export function fetchOrderList(data) {
    return (dispatch) => {
        return post('/api/activity/event/order/list',data).then(
            response => dispatch({
                type: types.FETCH_ORDER_LIST,
                orderList: response.data.array
            }),
            error => dispatch({
                type: types.FETCH_ORDER_LIST_FAILED,
                error
            })
        )
    }
}

/**
 * 获取订单详情
 * */
export function fetchOrderDetail(data) {
    return (dispatch) => {
        return post('/api/activity/event/order/one',data).then(
            response => dispatch({
                type: types.FETCH_ORDER_DETAIL,
                data: response
            }),
            error => dispatch({
                type: types.FETCH_ORDER_DETAIL_FAILED,
                error
            })
        )
    }
}

/**
 * 取消订单
 * */
export function cancelOrder(data) {
    return dispatch => {
        return post('/api/activity/event/order/cancel',data).then(
            response => dispatch({
                type: types.CANCEL_ORDER,
                data: response,
                code: response.code
            }),
            error => dispatch({
                type: types.CANCEL_ORDER_FAILED,
                error
            })
        )
    }
}


/**
 * 获取订单支付结果
 * */
export function fetchPayResult(data) {
    return dispatch => {
        return post('/api/activity/event/order/query',data).then(
            response => dispatch({
                type: types.FETCH_PAY_RESULT,
                data: response
            }),
            error => dispatch({
                type: types.FETCH_PAY_RESULT_FAILED,
                error
            })
        )
    }
}


/**
 * 获取微信签名
 * */
export function fetchWxSign(data) {
    return (dispatch) => {
        return post('/api/wechat/jsconfig',data).then(
            response => dispatch({
                type: types.FETCH_WX_SIGN,
                data: response
            }),
            error => dispatch({
                type: types.FETCH_WX_SIGN_FAILED,
                error
            })
        )
    }
}

/**
 * 获取微信支付
 * */
export function fetchWxPaySign(data) {
    return (dispatch) => {
        return post('/api/wechat/payconfig',data).then(
            response => dispatch({
                type: types.FETCH_WX_PAY_SIGN,
                data: response
            }),
            error => dispatch({
                type: types.FETCH_WX_PAY_SIGN_FAILED,
                error
            })
        )
    }
}

